//
//  PlacePickerView.m
//  EPBPro
//
//  Created by wucha on 2018/6/28.
//  Copyright © 2018年 fc. All rights reserved.
//

#import "PlacePickerView.h"
@interface PlacePickerView ()<UIPickerViewDelegate,UIPickerViewDataSource>{
    int chooseInteger;
    int chooseTwoInteger;
    int chooseThreeInteger;
}
@property (nonatomic,strong) UIView *confirmView;
@property (nonatomic,strong) NSArray *firstArray;
@property (nonatomic,strong) UIPickerView *pickerView;
//@property (nonatomic,strong) UIBarButtonItem *centerItem;
@property (nonatomic,strong) UILabel *centerLabel;
@property (nonatomic,strong) PlaceBlock placeBlock;
@end

@implementation PlacePickerView

-(id)initViewWithArray:(PlaceBlock)placeBlock{
    self = [super init];
    if (self) {
        self.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
        NSArray * array = [Utils getAllProvince];
        self.firstArray = array;
        [self ShowpickerViewWithSelectedIndex:0];
        self.placeBlock = placeBlock;
    }
    
    return self;
}

-(void)ShowpickerViewWithSelectedIndex:(int)index
{
    if(_confirmView!=nil) {
        [_confirmView removeFromSuperview];
    }
    
    _confirmView = [[UIView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT - 324+64, SCREEN_HEIGHT, 260)];
    _confirmView.backgroundColor=DefaultOpenColor;
    
    [self addSubview:_confirmView];
    UIView *toolbarView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 44)];
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftButton.titleLabel.font = [UIFont systemFontOfSize:14];
    leftButton.frame = CGRectMake(10, 0, 50, 44);
    [leftButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [leftButton setTitle:@"取消" forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(cancel:) forControlEvents:UIControlEventTouchUpInside];
    [toolbarView addSubview:leftButton];
    
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    rightButton.titleLabel.font = [UIFont systemFontOfSize:14];
    rightButton.frame = CGRectMake(SCREEN_WIDTH-60, 0, 50, 44);
    [rightButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [rightButton setTitle:@"完成" forState:UIControlStateNormal];
    [rightButton addTarget:self action:@selector(confirm:) forControlEvents:UIControlEventTouchUpInside];
    [toolbarView addSubview:rightButton];
//    UIBarButtonItem *fitItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    
    //    self.centerItem = fitItem;
    
    self.centerLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH-150, 24)];//pt-140,en-180
    _centerLabel.backgroundColor = [UIColor clearColor];//brownColor
    _centerLabel.textColor = [UIColor orangeColor];
    _centerLabel.font = [UIFont systemFontOfSize:16];
    _centerLabel.textAlignment = NSTextAlignmentCenter;
    _centerLabel.hidden = YES;
    
    [_confirmView addSubview:toolbarView];
    
    self.pickerView= [[UIPickerView alloc] initWithFrame:CGRectMake(0.0, 44,SCREEN_WIDTH, 216.0)];
    _pickerView.delegate = self;
    _pickerView.dataSource=self;
    _pickerView.backgroundColor = [UIColor whiteColor];
    _pickerView.showsSelectionIndicator = YES;
    //    if (self.chooseType == StartType) {
    //        [pickerView selectRow:startInteger?startInteger:0 inComponent:0 animated:YES];
    //    }else{
    //        [pickerView selectRow:endInteger?endInteger:0 inComponent:0 animated:YES];
    //    }
    [_confirmView addSubview:_pickerView];
}

#pragma mark- UIPickViewDelegate
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    
    return 3;
}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    if (component == 0) {
        return self.firstArray.count;
    }else if(component == 1){
        NSArray *array = self.firstArray[chooseInteger][@"children"];
        return array.count;
    }else{
        NSArray *array = self.firstArray[chooseInteger][@"children"];
        NSArray *array1 = array[chooseTwoInteger][@"children"];
        return array1.count;

    }
    return self.firstArray.count;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if(component == 0){
        NSDictionary *dic = self.firstArray[row];
        return dic[@"name"];
    }else if(component == 1){
        NSArray *array = self.firstArray[chooseInteger][@"children"];
        NSDictionary *dic = array[row];
        return dic[@"name"];
    }else{
        NSArray *array = self.firstArray[chooseInteger][@"children"];
        NSArray *array1 = array[chooseTwoInteger][@"children"];
        NSDictionary *dic = array1[row];
        return dic[@"name"];
    }
    
}

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if (component == 0) {
        chooseInteger = (int)row;
        chooseTwoInteger = 0;
        chooseThreeInteger = 0;
        [self.pickerView selectRow:0 inComponent:1 animated:NO];
        [self.pickerView selectRow:0 inComponent:2 animated:NO];
        [self.pickerView reloadAllComponents];

    }else if (component == 1){
        chooseTwoInteger = (int)row;
        [self.pickerView selectRow:0 inComponent:2 animated:NO];
        [self.pickerView reloadComponent:2];

    }else{
        chooseThreeInteger = (int)row;
    }
}

-(void)cancel:(id)sender{
    [self removeFromSuperview];
}

-(void)confirm:(id)sender{
//    if (wcBlock) {
//        wcBlock(chooseInteger);
//    }
//    if (wcDateBlock) {
//        if (self.dateString) {
//            wcDateBlock(self.dateString);
//        }
//    }
    if (self.placeBlock) {
        NSString *province = self.firstArray[chooseInteger][@"name"];
        NSArray *array = self.firstArray[chooseInteger][@"children"];
        NSString *city = array[chooseTwoInteger][@"name"];
        NSArray *array1 = array[chooseTwoInteger][@"children"];
        NSString *area = array1[chooseThreeInteger][@"name"];
        self.placeBlock(province, city, area);

    }
    [self removeFromSuperview];
    
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
